package com.kfyang.manualgear.daoplugin.completion;

import com.intellij.codeInsight.completion.*;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.util.PlatformIcons;
import org.jetbrains.annotations.NotNull;

public class SimpleCompletionContributor extends CompletionContributor {
//    public ParamCompletionContributor() {
//        System.out.println("aaa");
//        extend(CompletionType.BASIC, PlatformPatterns.psiElement(CustomHighlighterTokenType.IDENTIFIER),
//            new CompletionProvider<>() {
//                @Override
//                protected void addCompletions(@NotNull CompletionParameters completionParameters,
//                                              @NotNull ProcessingContext processingContext,
//                                              @NotNull CompletionResultSet resultSet) {
//                    System.out.println("basic");
//                    resultSet.addElement(LookupElementBuilder.create("hello"));
//                }
//            });
//    }

    @Override
    public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {

        if (parameters.getCompletionType() != CompletionType.BASIC)
            return;

        PsiElement position = parameters.getPosition();
//        PsiFile topLevelFile = InjectedLanguageManager.getInstance(position.getProject()).getTopLevelFile(position);
//        if (!shouldAddElement(position.getContainingFile(), position.getTextOffset())) {
//            return;
//        }

//        PsiFile topLevelFile =  InjectedLanguageManager.getInstance(position.getProject()).getTopLevelFile(position);
//        PsiElement element = topLevelFile.findElementAt(position.getTextOffset());
//        while (element != null) {
//            System.out.println(element);
//            element = element.getParent();
//        }
        
        if (shouldAddFrag(position.getContainingFile(), position.getTextOffset())) {

        } else {
            addCompletionForPsiParameter(result, position.getParent());
        }
    }

    private boolean shouldAddFrag(PsiFile file, int offset) {
        String text = file.getText();
        for (int i = offset - 1; i > 0; i--) {
            char ch = text.charAt(i);
            if (ch == '[' || ch == '`') {
                return true;
            }
            if (!((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_'))
                break;
        }
        return false;
    }

    private static void addCompletionForPsiParameter(@NotNull CompletionResultSet result, PsiElement element) {

        result.addElement(LookupElementBuilder.create("insert"));
        result.addElement(LookupElementBuilder.create("values"));
        result.addElement(LookupElementBuilder.create("update"));
        result.addElement(LookupElementBuilder.create("delete"));
        result.addElement(LookupElementBuilder.create("where"));
        result.addElement(LookupElementBuilder.create("group by"));
        result.addElement(LookupElementBuilder.create("order by"));
    }


    private static LookupElement buildLookupElementWithIcon(final String parameterName, final String parameterType) {
        return PrioritizedLookupElement.withPriority(
            LookupElementBuilder.create(parameterName)
                .withTypeText(parameterType)
                .withIcon(PlatformIcons.PARAMETER_ICON), 400);
    }
}
